< previous page page_178 next page >

Page 178
tip.gif
You can control the order of operations by using parentheses. For example, the following line of code assigns 12 to the variable result:
result = 15 - (6 - 3)

You can create assignment statements that consist solely of addition operators or solely of subtraction operators. You can also use the operators in combination with one another or other math operators. The following code lines show a few valid math operations:
val1 = 1.25 + 3.17
val2 = 3.21 - 1
val3 = val2 + val1
val4 = val3 + 3.75 - 2.1 + 12 - 3
val4 = val4 + 1
If you are not familiar with computer programming, the last line (in which the same variable name appears both on the right and left of the equal sign) might look a little strange to you. In fact, that line is not allowed in some programming languages. However, in Visual Basic, you can enter a line of code that tells the program to take the current value of a variable, add another number to it, and then store the resulting value back in the same variable.
Multiplication and Division
Two other major mathematical operations with which you should be familiar are multiplication and division. Like addition and subtraction, these operations are used frequently in everyday life.
Using the Multiplication Operator
Multiplication in Visual Basic is straightforward, just like addition and subtraction. You simply use the multiplication operatorthe asterisk (*) operatorto multiply two or more numbers. The syntax of a multiplication statement, which follows, is almost identical to the ones for addition and subtraction:
result = number1 * number2 [* number3]
As before, result is the name of a variable used to contain the product of the numbers being multiplied, and number1,number2, and number3 are the literal numbers, numeric variables, or functions.
Using the Division Operators
Division in Visual Basic is a little more complicated than multiplication. In Listing 9.1, you see how one type of division is used. This division is what you are most familiar with and what you will find on your calculator. This type of division returns a number with its decimal portion, if one is present.
However, this type is only one of three different types of division supported by Visual Basic. They are known as floating-point division (the normal type of division, with which you are familiar); integer division; and modulus, or remainder, division.
Floating-point division is the typical division that you learned in school. You divide one number by another, and the result is a decimal number. The floating-point division operator is the forward slash (/):

 
< previous page page_178 next page >